import { Component, OnInit, Injector } from '@angular/core';
import { ServiceProxy } from '@shared/service-proxy/service-proxies';
import { NzTableHelper } from '@shared/helpers/NzTableHelper';
import { finalize } from 'rxjs/operators';
import { AppComponentBase } from '@shared/common/app-component-base';

@Component({
  selector: 'app-fix-reader',
  templateUrl: './fix-reader.component.html',
  styleUrls: ['./fix-reader.component.less']
})
export class FixReaderComponent extends AppComponentBase implements OnInit {

  tableHelper: NzTableHelper;
  isVisible: boolean = false;
  keyword: string = '';
  statistics: any;
  constructor(
    injector: Injector,
    private _service: ServiceProxy) {
    super(injector);
    this.tableHelper = new NzTableHelper();
  }


  ngOnInit() {
    this.load();
    this.statistics = { unAllot: "", inTransit: "", totalCount: "", siteName: "" };
  }

  load(pageIndex?: number) {
    if (typeof pageIndex !== 'undefined') {
      this.tableHelper.pageIndex = pageIndex || 1;
    }
    this.getList();
    this.getStatistics();
  }

  sort(sort: { key: string, value: string }): void {
    this.tableHelper.setSorting = sort;
    this.load();
  }

  getList() {
    this._service.getPageFixedReadingDevicesUsingGET(this.tableHelper.pageIndex, this.tableHelper.pageSize, this.keyword
    ).pipe(finalize(() => {
      this.tableHelper.loading = false;
    })).subscribe((result) => {
      this.tableHelper.items = result.result.fieldList;
      this.tableHelper.totalCount = result.result.totalRow;
    });
  }

  getStatistics() {
    this._service.getFixdevicestatisticsUsingGET().subscribe(result => {
      this.statistics = result.result;
    })
  }

  reset() {
    this.tableHelper.pageIndex = 1;
    this.keyword = '';
    this.load();
  }
}
